define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'school_calss_user/index?class_id=' + Config.class_id + location.search,
                    add_url: 'school_calss_user/add?class_id=' + Config.class_id,
                    edit_url: 'school_calss_user/edit',
                    del_url: 'school_calss_user/del',
                    multi_url: 'school_calss_user/multi',
                    table: 'school_calss_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'nickname', title: __('Nickname')},
                        {field: 'mobile', visible: Config.mobiles,title: __('Mobile')},
                        {field: 'total_class_hour', title: __('Total_class_hour')},
                        {field: 'use_class_hour', title: '已用课时'},
                        {
                            field: 'duty_time',
                            title:  '入班时间',
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'createtime',
                            title: '创建时间',
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'status',
                            title: '学生状态',
                            searchList: {"0": '学习中', "1": '毕业', "2": '退班'},
                            formatter: Table.api.formatter.status
                        },
                      /*  {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate
                        }, */
                        {
                            field: 'buttons',
                            width: "120px",
                            title: '其他操作',
                            table: table,
                            operate: false,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.buttons,
                            buttons: [
                                {
                                    name: 'ajax',
                                    text: '退班',
                                    title: '退班',
                                    classname: 'btn btn-xs btn-success btn-magic btn-ajax',
                                    url: 'school_calss_user/leave_work',
                                    confirm: '确认退班?',
                                    visible: function(row){
                                        if(row.status == '0'){
                                            return true;
                                        }else{
                                            return false;
                                        }
                                    },
                                    success: function (data, ret) {
                                        Layer.alert(ret.msg);
                                        table.bootstrapTable('refresh');
                                    }
                                },
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});